<?php

namespace App\Http\Controllers\Mini\Shop;

use App\Http\Controllers\Controller;
use App\Http\Resources\Home\OrderCommentResource\OrderCommentCollection;
use App\Http\Resources\Home\OrderCommentResource\OrderCommentResource;
use App\Models\OrderComment;
use App\Services\OrderCommentService;
use App\Services\UploadService;
use App\Services\UserService;
use Illuminate\Http\Request;

class OrderCommentController extends Controller
{
    /**
     * 用户评论列表
     * @param Request $request
     * @return array
     */
    public function index(Request $request)
    {
        $user_service = new UserService();
        $user_info = $user_service->getUserInfo();
        $oc_model = new OrderComment();
        $list = $oc_model->with(['goods' => function ($q) {
            return $q->select('id', 'goods_name', 'goods_master_image');
        }])->where('user_id', $user_info['id'])->paginate($request->per_page ?? 30);
        return $this->format(new OrderCommentCollection($list));

    }

    /**
     * 2021/3/12--18:15
     * @param Request $request
     * @return array
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:store
     * @effect:添加评论
     */
    public function store(Request $request)
    {
        $oc_service = new OrderCommentService();
        $rs = $oc_service->add();
        return $rs['status'] ? $this->success($rs['data'], $rs['msg']) : $this->error($rs['msg']);
    }

    /**
     * 2021/3/12--18:15
     * @param $id
     * @return array
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:show
     * @effect:查看评论
     */
    public function show($id)
    {
        $user_service = new UserService();
        $user_info = $user_service->getUserInfo();
        $oc_model = new OrderComment();
        $list = $oc_model->with(['goods' => function ($q) {
            return $q->select('id', 'goods_name', 'goods_master_image');
        }])->where('user_id', $user_info['id'])->where('id', $id)->first();
        return $this->success(new OrderCommentResource($list));
    }

    /**
     * 2021/3/12--18:15
     * @param Request $request
     * @param $id
     * @return array
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:update
     * @effect:修改评论
     */
    public function update(Request $request, $id)
    {
        $oc_service = new OrderCommentService();
        $rs = $oc_service->edit($id);
        return $rs['status'] ? $this->success($rs['data'], $rs['msg']) : $this->error($rs['msg']);
    }


    /**
     * 2021/3/19--17:43
     * @param UploadService $upload_service
     * @return array
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:comment_upload
     * @effect:评论图片上传
     */
    public function comment_upload(UploadService $upload_service)
    {
        $user_service = new UserService();
        $user_info = $user_service->getUserInfo();
        $rs = $upload_service->comment($user_info['id']);
        if ($rs['status']) {
            return $this->success($rs['data'], $rs['msg']);
        } else {
            return $this->error($rs['msg']);
        }
    }
}
